package com.kpf.springcloud.utils;

import com.kpf.springcloud.common.ResponseEnum;
import com.kpf.springcloud.exception.BusinessException;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author shiwen
 * @date 2021/2/16
 */
public class GeneralUtil {

    public static boolean isNull(Object obj) {
        return null == obj;
    }

    public static boolean isNullOrEmpty(String s) {
        return null == s || s.isEmpty();
    }

    public static boolean isNotNullAndEmpty(String s) {
        return !isNullOrEmpty(s);
    }

    public static boolean isNotNull(Object obj) {

        return !isNull(obj);
    }

    /**
     * @param obj 输入对象 (Object,Long,String...等对象)
     * @return 不为空返回 true, 否则返回false
     * @Title: isObjNotNull
     * @Description: (判断Object 是否为空, 常用于字符串的判断)
     * @author shiwen
     */
    public static boolean isObjNotNull(Object obj) {
        return !isObjNull(obj);
    }

    public static boolean isNotNullOrEmpty(String s) {
        return !isNullOrEmpty(s);
    }

    public static boolean isObjNull(Object obj) {
        return obj == null || "".equals(obj.toString().trim());
    }

    /**
     * @param list
     * @return 如果list不为空 返回 true, 为空返回 false
     * @Title: isListNotNull
     * @Description: (判断 list 是否为空)
     * @author shiwen
     */
    public static boolean isListNotNull(List list) {
        if (list != null && !list.isEmpty()) {
            return true;
        }
        return false;
    }

    public static boolean isListNull(List list) {
        return !isListNotNull(list);
    }

    public static boolean isObjNullOrZero(Object obj) {
        return isObjNull(obj) || "0".equals(obj.toString());
    }

    public static boolean isObjNotNullAndZero(Object obj) {
        return !isObjNullOrZero(obj);
    }

    /**
     * 手机号脱敏 （中间四位隐藏）
     * @param mobile 手机号
     * @return 脱敏后的手机号
     */
    public static String mobile(String mobile) {
        if (StringUtils.isEmpty(mobile)) { return ""; }
        char[] chars = mobile.toCharArray();
        // 字符串转字符再替换里面的内容, 最后再重新转换为字符串
        int i = 3;
        chars[i ++] = '*';
        chars[i ++] = '*';
        chars[i ++] = '*';
        chars[i] = '*';
        return new String(chars);
    }

}